package com.systop.party08.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.systop.party08.R;
import com.systop.party08.activity.ArticleDetailActivity;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.HomeEntity;
import com.systop.party08.utils.GlideImageLoader;
import com.systop.party08.utils.ToastUtils;
import com.systop.party08.view.NestingListView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.COMIMG;

/**
 *
 */
public class HomeHomeFragment extends BaseFragment {

    @BindView(R.id.fragment_home_home_banner)
    Banner banner;
    @BindView(R.id.fragment_home_home_tv_banner)
    TextView tvBanner;
    @BindView(R.id.fragment_home_home_nlv)
    NestingListView nlv;
    @BindView(R.id.fragment_home_home_sv)
    ScrollView sv;

    private View view;
    private HomeEntity.DataBeanX dataBeanX;

    public static HomeHomeFragment newInstance(HomeEntity.DataBeanX dataBeanX) {
        HomeHomeFragment fragment = new HomeHomeFragment();
        Bundle args = new Bundle();
        args.putSerializable("dataBeanX", dataBeanX);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            dataBeanX = (HomeEntity.DataBeanX) getArguments().getSerializable("dataBeanX");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_home, container, false);
        ButterKnife.bind(this, view);
        initBanner();
        initNlv();
        return view;
    }

    private void initBanner() {
        ArrayList<String> listTitle = new ArrayList<>();
        ArrayList<String> listImages = new ArrayList<>();
        List<HomeEntity.DataBeanX.JdwzListBean> jdwzListBeans = dataBeanX.getJdwzList();
        if (jdwzListBeans != null && jdwzListBeans.size() > 0) {
            tvBanner.setText(jdwzListBeans.get(0).getTitle());
            for (HomeEntity.DataBeanX.JdwzListBean item : jdwzListBeans) {
                listTitle.add(item.getTitle());
                listImages.add(COMIMG + item.getFocusPic());
            }
        }
        banner.setBannerTitles(listTitle);
        banner.setImages(listImages);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置自动轮播，默认为true
        banner.isAutoPlay(false);
        //设置轮播时间
        banner.setDelayTime(2000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
              // 页面滑动过程中的回调  position当前正在滑动的页面索引  positionOffset滑动偏移比例(0.0-1.0)  positionOffsetPixels滑动偏移像素值
            }

            @Override
            public void onPageSelected(int position) {
                //页面完全停止滑动后触发一次
                tvBanner.setText(listTitle.get(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //滑动状态发生变化时触发
                //state ViewPager.SCROLL_STATE_IDLE(0)：滑动停止
                //ViewPager.SCROLL_STATE_DRAGGING(1)：手指拖动中
                //ViewPager.SCROLL_STATE_SETTLING(2)：自动滑动中
            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //跳转详情
                ToastUtils.getInstance(getActivity()).showMessage("点击了第"+position+"张图片");
            }
        });
    }

    private void initNlv() {
        List<HomeEntity.DataBeanX.WzListBean> wzList = dataBeanX.getWzList();
        CommonAdapter<HomeEntity.DataBeanX.WzListBean> adapter = new CommonAdapter<HomeEntity.DataBeanX.WzListBean>(getActivity(), R.layout.item_home_article, wzList) {
            @Override
            protected void convert(ViewHolder viewHolder, HomeEntity.DataBeanX.WzListBean item, int position) {
                viewHolder.setText(R.id.item_home_article_tv, item.getLnmc());
                NestingListView lv = viewHolder.getView(R.id.item_home_article_nlv);
                List<HomeEntity.DataBeanX.WzListBean.DataBean> data = item.getData();
                lv.setAdapter(new CommonAdapter<HomeEntity.DataBeanX.WzListBean.DataBean>(getActivity(), R.layout.item_article, data) {
                    @Override
                    protected void convert(ViewHolder viewHolder, HomeEntity.DataBeanX.WzListBean.DataBean item, int position) {
                        viewHolder.setText(R.id.item_article_tv_title, item.getTitle());
                        viewHolder.setText(R.id.item_article_tv_date, item.getPubDate());
                    }
                });
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //跳转文章详情页
//                        Intent intent = new Intent(getActivity(), ArticleDetailActivity.class);
//                        intent.putExtra("id", data.get(position).getId());
//                        intent.putExtra("type", data.get(position).getLx());
//                        startActivity(intent);
                        FragmentActivity activity=getActivity();
                        ToastUtils.getInstance(activity).showMessage("点击了"+item.getLnmc()+"的第"+position+"条消息");

                    }
                });
            }
        };
        nlv.setAdapter(adapter);
        sv.smoothScrollTo(0, 0);
    }

}